package com.haier.a920changjingyanshidemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.haier.a920changjingyanshidemo.fragment.EnjoyHeatFragment;
import com.haier.a920changjingyanshidemo.fragment.GetWaterFragment;

import java.lang.reflect.InvocationTargetException;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup bottomRadioGroup;
    private FragmentManager supportFragmentManager;
    private Fragment showFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);
        supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        showFragment = new EnjoyHeatFragment();
        fragmentTransaction.add(R.id.fragment_layout, showFragment, GetWaterFragment.TAG);
        fragmentTransaction.commit();
        initView();


    }

    /*初始化view*/
    private void initView() {

        bottomRadioGroup = (RadioGroup) findViewById(R.id.bottom_radiogroup);
        bottomRadioGroup.setOnCheckedChangeListener(this);
        bottomRadioGroup.check(((RadioButton) bottomRadioGroup.getChildAt(1)).getId());
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_heat:

                /*全屋采暖*/
                showFragment(EnjoyHeatFragment.TAG, EnjoyHeatFragment.class);
                break;
            case R.id.rb_water:
                /*全屋用水*/
                showFragment(GetWaterFragment.TAG, GetWaterFragment.class);

                break;
            case R.id.rb_exper:

                /*空气体验*/
//                showFragment(ExperienceFragment.TAG, ExperienceFragment.class);

                break;
        }
    }


    public void showFragment(String TAG, Class<? extends Fragment> cls) {
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.hide(showFragment);
        showFragment = supportFragmentManager.findFragmentByTag(TAG);


        if (showFragment != null) {

            fragmentTransaction.show(showFragment);

        } else {
            try {
                showFragment = cls.getConstructor().newInstance();
                fragmentTransaction.add(R.id.fragment_layout, showFragment, TAG);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        }
        fragmentTransaction.commitAllowingStateLoss();
    }
}
